local mingsong = fk.CreateSkill{
    name = "wk_heg__mingsong"
}

local H = require "packages.ol_hegemony.util"

Fk:loadTranslationTable{
    ["wk_heg__mingsong"] = "明讼",
    [":wk_heg__mingsong"] = "与你势力相同的角色造成伤害时，你可移动其装备区里的一张武器牌，防止此伤害，然后你可令一名体力值最小的角色回复1点体力。",

    ["#wk_heg__mingsong-ask"] = "明讼：你可选择一名角色，将 %dest 装备区内的一张武器牌移动至其装备区内，<br />防止此伤害并令一名体力值最小的角色回复1点体力",
    ["#wk_heg__mingsong-choose"] = "明讼：你可令一名体力值最小的角色回复1点体力",

    ["$wk_heg__mingsong1"] = "你我推心置腹，岂能相负。",
    ["$wk_heg__mingsong2"] = "孰忠孰奸，朕尚能明辨。",
}

mingsong:addEffect(fk.DetermineDamageCaused,{
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if not (player:hasSkill(mingsong.name) and target and H.compareKingdomWith(player, target)) then return false end
    local cards = table.simpleClone(target:getEquipments(Card.SubtypeWeapon))
    local targets = {}
    if #cards > 0 then
      for _, id in ipairs(cards) do
        for _, q in ipairs(player.room:getOtherPlayers(target)) do
          if target:canMoveCardInBoardTo(q, id) then
            table.insertIfNeed(targets, q)
          end
        end
      end
    end
    if #targets > 0 then
      event:setCostData(self,{targets = targets})
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).targets
    local tos = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = mingsong.name,
        prompt = "#wk_heg__mingsong-ask::"..target.id,
        cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self,{to = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to[1]
    local excludeIds = table.filter(target:getCardIds("e"), function (id) return Fk:getCardById(id).sub_type ~= Card.SubtypeWeapon end)
    room:askToMoveCardInBoard(player,{
        target_one = target,
        target_two = to,
        skill_name = mingsong.name,
        flag = "e",
        move_from = target,
        exclude_ids = excludeIds,
    })
    local targets = {}
    local num = 999
    for _, p in ipairs(room.alive_players) do
      local n = p.hp
      if n <= num then
        if n < num then
          num = n
          targets = {}
        end
        if n < p.maxHp then
          table.insertIfNeed(targets, p)
        end
      end
    end
    if #targets > 0 then
      local to_heal = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = mingsong.name,
        prompt = "#wk_heg__mingsong-choose",
        cancelable = true,
      })
      if #to_heal > 0 then
        room:recover{
          who = to_heal[1],
          num = 1,
          recoverBy = player,
          skillName = mingsong.name,
        }
      end
    end
    data:preventDamage()
  end,
})

return mingsong